package com.ldatum.crm.action.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.ldatum.crm.domain.Admin;
import com.ldatum.crm.domain.Level;
import com.ldatum.crm.domain.Member;
import com.ldatum.crm.service.LevelService;
import com.ldatum.crm.service.MemberService;
import com.ldatum.crm.serviceImpl.LevelServiceImpl;
import com.ldatum.crm.serviceImpl.MemberServiceImpl;
@ParentPackage("admin")
public class MemberAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private MemberService memberService = new MemberServiceImpl();
	private LevelService levelService = new LevelServiceImpl();
	
	private Member member;
	
	private int id;
	private String cardno;
	private String level;
	private String name;
	private String gender;
	private String birthday;
	private String address;
	private String telephone;
	private String email;
	private String joindate;
	private String checker;
	private double discount;
	
	private String ids; //接收前台传递过来的会员卡号串
	private String cardnoOrTel;//接收前台传递过来的卡号或者手机号码
	
	
	private List<Level> levelList = new ArrayList<Level>();
	private List<Member> memberList = new ArrayList<Member>(); 
	
	/**
	 * 
	 * 描述 : <会员登记,记录会员的信息>.
	 * @return
	 * @throws Exception
	 */
	public String checkin() throws Exception{
		levelList = levelService.list();
		return "check-in";
	}
	
	/**
	 * 保存会员信息
	 * @throws IOException
	 */
	public void save() throws IOException{
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			PrintWriter out;
			out = response.getWriter();
			JSONObject json = new JSONObject();
			Member mem = new Member();
			mem.setId(id);
			mem.setAddress(address);
			mem.setCardno(cardno);
			mem.setGendar(gender);
			mem.setLevel(level);
			mem.setName(name);
			mem.setPhone(telephone);
			mem.setBirthday(birthday);
			mem.setChecker((String) getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME));
			mem.setEmail(email);
			mem.setJoindate(joindate);
			int result;
			boolean b = memberService.validationCardno(cardno);
			if(b){
				result = memberService.update(mem);
			}else{
				result = memberService.save(mem);
			}
			if(result==1){
				json.accumulate("success", true);
			}else{
				json.accumulate("success", false);
			}
			out.println(json.toString());
			out.flush();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 
	 * 描述 : <会员级别信息增加，删除，修改>.
	 * @return
	 * @throws Exception 
	 */
	public String memberLevel() throws Exception{
		levelList = levelService.list();
		return "listLevel";
	}
	
	/**
	 * 
	 * 描述 : <会员列表>.
	 * @return 
	 */
	public String memberList(){
		try {
			memberList = memberService.list();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "listMember";
	}
	
	/**
	 * 
	 * 描述 : <会员信息>.
	 * @return
	 */
	public String memberInfo() throws Exception{
		Member member = new Member();
		try {
			if(id!=0){
				member = memberService.findById(id);
			}else if(cardnoOrTel!=""){
				member = memberService.findByCardno(cardnoOrTel);
			}
			level = member.getLevel();
			name = member.getName();
			gender = member.getGendar();
			birthday = member.getBirthday();
			address = member.getAddress();
			telephone = member.getPhone();
			email  = member.getEmail();
			checker = member.getChecker();
			joindate = member.getJoindate();
			cardno = member.getCardno();
			levelList = levelService.list();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "editMember";
	}
	/**
	 * 验证会员卡号是否存在
	 * @return
	 * @throws IOException 
	 * @throws SQLException 
	 */
	public void validationCardno() throws IOException, SQLException{
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			PrintWriter out;
			out = response.getWriter();
			JSONObject json = new JSONObject();
			boolean b = memberService.validationCardno(cardno);
			json.accumulate("result", b);
			out.println(json.toString());
			out.flush();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void validationTel() throws IOException, SQLException{
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			PrintWriter out;
			out = response.getWriter();
			JSONObject json = new JSONObject();
			boolean b = memberService.validationTel(telephone);
			json.accumulate("result", b);
			out.println(json.toString());
			out.flush();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void validationCardnoOrTel() throws Exception{
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			PrintWriter out;
			out = response.getWriter();
			JSONObject json = new JSONObject();
			boolean b = memberService.validationCardnoOrTel(cardnoOrTel);
			json.accumulate("result", b);
			if(b){
				member = memberService.findByCardno(cardnoOrTel);
				json.accumulate("cardno",cardnoOrTel);
				json.accumulate("name", member.getName());
				json.accumulate("level", member.getLevel());
				json.accumulate("joindate", member.getJoindate());
				json.accumulate("discount", levelService.findByLevel(member.getLevel()).getDiscount());
			}
			out.println(json.toString());
			out.flush();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除会员数据
	 * @return
	 * @throws SQLException 
	 * @throws IOException 
	 */
	public void deletes() throws SQLException, IOException{
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			PrintWriter out;
			out = response.getWriter();
			JSONObject json = new JSONObject();
			int result  = memberService.deletes(ids);
			if(result<1){
				json.accumulate("success", false);
			}else{
				json.accumulate("success", true);
			}
			out.println(json.toString());
			out.flush();
			out.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 
	 * 描述 : 批量更新会员的状态
	 */
	public void updateStatusToStoping(){
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			PrintWriter out;
			out = response.getWriter();
			JSONObject json = new JSONObject();
			int result  = memberService.updateStatusToStoping(ids);
			if(result<1){
				json.accumulate("success", false);
			}else{
				json.accumulate("success", true);
			}
			out.println(json.toString());
			out.flush();
			out.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void updateStatusToStart(){
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			PrintWriter out;
			out = response.getWriter();
			JSONObject json = new JSONObject();
			int result  = memberService.updateStatusToStart(ids);
			if(result<1){
				json.accumulate("success", false);
			}else{
				json.accumulate("success", true);
			}
			out.println(json.toString());
			out.flush();
			out.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public String getCardno() {
		return cardno;
	}

	public void setCardno(String cardno) {
		this.cardno = cardno;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public List<Level> getLevelList() {
		return levelList;
	}

	public void setLevelList(List<Level> levelList) {
		this.levelList = levelList;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public List<Member> getMemberList() {
		return memberList;
	}

	public void setMemberList(List<Member> memberList) {
		this.memberList = memberList;
	}

	public String getJoindate() {
		return joindate;
	}

	public void setJoindate(String joindate) {
		this.joindate = joindate;
	}

	public String getChecker() {
		return checker;
	}

	public void setChecker(String checker) {
		this.checker = checker;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getCardnoOrTel() {
		return cardnoOrTel;
	}

	public void setCardnoOrTel(String cardnoOrTel) {
		this.cardnoOrTel = cardnoOrTel;
	}

	public double getDiscount() {
		return discount;
	}

	public void setDiscount(double discount) {
		this.discount = discount;
	}
	
}
